package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.service.AppOrderRebateService;
import cn.stylefeng.guns.modular.system.service.ProxyUserService;
import cn.stylefeng.guns.modular.system.utils.ResultVOUtil;
import cn.stylefeng.guns.modular.system.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderRebateController
 * @Author lzy
 * @Date 2020/1/12 3:39
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/orderRebate")
public class OrderRebateController {
    private final static Logger log = LoggerFactory.getLogger(OrderRebateController.class);
    private String PREFIX = "/modular/system/orderRebate/";

    /**
     * 跳转到主页面
     *
     * @author wg
     * @Date 2019-06-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "orderRebate.html";
    }

    @Autowired
    private AppOrderRebateService appOrderRebateService;

    @Autowired
    private ProxyUserService proxyUserService;

    /**
     * 回款列表(条件查询)
     *      *  userId
     * @param startTime 查询回款时间rebateTime的区间
     * @param endTime
     * @param orderNumber   根据订单编号——orderNumber模糊查询
     * @param money 回款金额
     * @param status    状态 1,待回款 2.待支付 3.用户确认 4.监控确认 5.平台确认 9.超时 10.取消
     * @param pageNum
     * @return
     */
    @RequestMapping("/selectMyOrderRebate")
    @ResponseBody
    public ResultVO selectMyOrderRebate(@RequestParam(value = "startTime", required = false) String startTime,
                                        @RequestParam(value = "endTime", required = false) String endTime,
                                        @RequestParam(value = "orderNumber", required = false) String orderNumber,
                                        @RequestParam(value = "money", required = false) String money,
                                        @RequestParam(value = "status", required = false) String status,
                                        @RequestParam("page")Integer pageNum, @RequestParam("limit") Integer pageSize
                                        ){
        Long userId = LoginContextHolder.getContext().getUserId();
        Assert.isTrue(!ObjectUtils.isEmpty(userId), ResultEnum.PROXYID_DOES_NOT_EXIST.getMessage());

        //查询所有的子代理
        List<String> subUserIds = proxyUserService.findSubUserIdList(userId.toString());
        //查询用户代理中加入自己的id
        subUserIds.add(userId.toString());

        PageHelper.startPage(pageNum,pageSize);
        List<Map<Object,Object>> syss = appOrderRebateService.findListByCondition(startTime,endTime,orderNumber,money,status,userId,subUserIds);
        PageInfo<Map<Object,Object>> pageInfo = new PageInfo<>(syss);
        List<Map<Object, Object>> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        return ResultVOUtil.success(list,total);
    }

}
